package Algorithm.math;

/**
 * @author liujun
 * @version 1.0
 * @date 2020/5/15
 * @author—Email ljfirst@mail.ustc.edu.cn
 * @blogURL https://blog.csdn.net/ljfirst
 * @description 大整数加法
 * 1、输入检查
 * 2、关于0的处理
 * 3、运算符同号判断
 * 4、小数的处理
 * 5、进行运算：两个正数相加、正数加负数、负数加正数、负数加负数
 * 6、还原小数【注意最后输出的数字最后一位是否为小数点】
 */
public class ArrayBigIntegersSum extends ArrayBigIntegersOperation {

    //大整数加法
    public String sumWithSymbol(String args, String args2) {
        String sum = "0";
        if (!OperationCheck(args, args2)) {
            return sum;
        }
        // 关于0的处理
        if (args.equals("0") || args2.equals("0")) {
            return args.equals("0") ? args2 : args;
        }

        //运算符同号判断
        boolean signal = true;
        boolean signal2 = true;
        if (args.charAt(0) == '-') {
            args = args.substring(1);
            signal = false;
        }
        if (args2.charAt(0) == '-') {
            args2 = args2.substring(1);//tips:substring是左闭右开
            signal2 = false;
        }
        int position = 0;

        // 小数的处理
        if (args.contains(".") || args2.contains(".")) {
            int pointPosition = args.indexOf(".");
            int pointPosition2 = args2.indexOf(".");

            StringBuilder argsBuilder = new StringBuilder(args);
            if (pointPosition != -1) {
                argsBuilder.deleteCharAt(pointPosition);
            } else {
                pointPosition = args.length() - 1;
            }
            StringBuilder argsBuilder2 = new StringBuilder(args2);
            if (pointPosition2 != -1) {
                argsBuilder2.deleteCharAt(pointPosition2);
            } else {
                pointPosition2 = args2.length();
            }

            // 注意此处是 argsBuilder.length
            while (pointPosition < argsBuilder.length() || pointPosition2 < argsBuilder2.length()) {
                if (pointPosition >= argsBuilder.length()) {
                    argsBuilder.append("0");
                }
                if (pointPosition2 >= argsBuilder2.length()) {
                    argsBuilder2.append("0");
                }
                position++;
                pointPosition++;
                pointPosition2++;
            }
            args = argsBuilder.toString();
            args2 = argsBuilder2.toString();
        }

        // 进行运算：两个正数相加、正数加负数、负数加正数、负数加负数
        if (signal) {
            if (signal2) {
                sum = sumWithoutSymbol(args, args2);
            } else {
                sum = subWithoutSymbol(args, args2);//signal == 0 && signal2 == 1
            }
        } else {
            if (signal2) {
                sum = subWithoutSymbol(args2, args);//signal == 1 && signal2 == 0
            } else {
                sum = "-" + sumWithoutSymbol(args2, args);//signal == 1 && signal2 == 1
            }
        }

        // 还原小数
        if (position != 0) {
            StringBuilder sb = new StringBuilder(sum);
            sb.insert(sum.length() - position, ".");
            int length = sb.length() - 1;
            while (sb.charAt(length) != '.' && sb.charAt(length) == '0') {
                sb.deleteCharAt(length);
                length--;
            }
            // 注意最后一个数字是"."
            sum = sb.charAt(length) != '.' ? sb.toString() : sb.deleteCharAt(length).toString();
        }
        return sum;
    }
}
